package com.dome.common.pojo;

import com.dome.common.enumeration.DomeErrorCode;
import lombok.Data;

/**
 * 通用响应格式
 * @param <DATA>
 */
@Data
public class DomeResponse<DATA> {

    /**
     * 响应码
     */
    private int errorCode;

    /**
     * 响应码对应的内容
     */
    private String errorMsg;

    /**
     * 数据集
     */
    private DATA data;

    public static <T> DomeResponse<T> success(T data) {
        return of(DomeErrorCode.SUCCESS, data);
    }

    public static DomeResponse<String> success(int errorCode, String errorMsg) {
        DomeResponse res = new DomeResponse();
        res.setErrorCode(errorCode);
        res.setErrorMsg(errorMsg);
        return res;
    }

    public static DomeResponse<String> fail(DomeErrorCode errCode) {
        return of(errCode, null);
    }

    public static <T> DomeResponse<T> fail(String errorMsg,T data) {
        DomeResponse res = new DomeResponse();
        res.setErrorCode(DomeErrorCode.SYSTEM_ERROR.getCode());
        res.setErrorMsg(errorMsg);
        res.setData(null);
        return res;
    }

    public static DomeResponse<String> fail(int errorCode, String errorMsg) {
        DomeResponse res = new DomeResponse();
        res.setErrorCode(errorCode);
        res.setErrorMsg(errorMsg);
        return res;
    }

    public static <T> DomeResponse<T> of(DomeErrorCode errCode, T data) {
        DomeResponse<T> res = new DomeResponse<T>();
        res.setErrorCode(errCode.getCode());
        res.setErrorMsg(errCode.getDesc());
        res.setData(data);
        return res;
    }

    public static <T> DomeResponse<T> of(int errorCode, String errorMsg, T data) {
        DomeResponse<T> res = new DomeResponse<T>();
        res.setErrorCode(errorCode);
        res.setErrorMsg(errorMsg);
        res.setData(data);
        return res;
    }


}
